// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App.vue";
import ElementUI from "element-ui";
import "../node_modules/element-ui/lib/theme-chalk/index.css";
import './directives'
import '../theme/index.css'
import router from "./router";
import 'echarts-liquidfill/src/liquidFill.js'
// 引用API文件
import api from "./api/index.js";
import config from "./config/config.js";
import i18n from "./lang";



// 将API方法绑定到全局
router.beforeEach((to, from, next) => {
  // console.log(to,'to');
  // console.log(from,'from');
  Vue.prototype.$Bus.$emit('Loading',config.defaultLoading);
  if(to.path == from.path) Vue.prototype.$Bus.$emit('Loading',false);

  //设置页面title
  if(to.meta.title){
    document.title = to.meta.title + " | " + config.system.name + " " + config.system.note;
  }else{
    document.title = config.system.name + " " + config.system.note;
  }
  //判断是注册页面
  if(to.meta.IsRegistration){
    // console.log(to.path);
    return next()
  }

  if (!to.meta.NoLogin && config.Login) {
    // 查看是否登陆
    if (localStorage.token) {
      // 没登录的做处理
      api.get('/noauth/refreshToken', {
        authorization: "IntForce " + localStorage.token
      }, function (data) {
        if(data.data && data.data.msg == 'success'){
          sessionStorage.active = to.name
          if(sessionStorage.isAlreadyUser == 1){
            next()
          }else{
            api.get('/sys/user/cur-user', {},
              function (data) {
                if (data && data.msg == 'success') {
                  if (data.data.orgAuthVos) {
                    let orgAuthVos = data.data.orgAuthVos
                    let str = orgAuthVos[0].company_name ? orgAuthVos[0].company_name + '-' : '';
                    str += orgAuthVos[0].org_auth_name;
                    localStorage.orgAuthVos = orgAuthVos ? JSON.stringify(orgAuthVos) : '';
                    localStorage.headName = str;
                  } else if (data.data.roleVoList && data.data.roleVoList.length) {
                    localStorage.headName = data.data.roleVoList[0].role_name;
                  } else {
                    ElementUI({type:"error", message: '没有任何权限，请联系相关人员！', showClose: true});
                    localStorage.headName = "";
                    Vue.prototype.$Bus.$emit('Loading',false);
                    next({
                      path: "/login"
                    });
                  }
                  localStorage.userName = data.data.user_name;
                  localStorage.isRoot = data.data.roleVoList[0].id == 'ROOT' ? 1 :0;
                  localStorage.isSSO = data.data.is_sso
                  localStorage.logoutUrl = data.data.logout_url ? data.data.logout_url : ''
                  sessionStorage.isAlreadyUser = 1
                }
                // console.log('2');
                next()
              },function (data) {
                Vue.prototype.$Bus.$emit('Loading',false);
                next({
                  path: "/login"
                });
              })
          }
        }else{
          Vue.prototype.$Bus.$emit('Loading',false);
          next({
            path: "/login"
          });
        }
      }, function (err) {
        Vue.prototype.$Bus.$emit('Loading',false);
        next({          
          path: "/login"
        });
      })

    } else {
      Vue.prototype.$Bus.$emit('Loading',false);
      // 登陆的正常跳
      next({
        path: "/login"
      });
    }
  } else {
    Vue.prototype.$Bus.$emit('Loading',false);
    // 不需要鉴权的跳转
    // console.log('3');
    next()
  }

});

Vue.prototype.$api = api;
Vue.prototype.$Bus = new Vue({});
Vue.prototype.router = router;
Vue.use(ElementUI,{
  size:'medium',
  i18n:(key,value) => i18n.t(key,value)
})

new Vue({
  el: "#app",
  router,
  i18n,
  template: "<App/>",
  components: {App}
});
